/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.unittests.tests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.location.AddressInfo;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Landmark;
import javax.microedition.location.LandmarkException;
import javax.microedition.location.QualifiedCoordinates;

import com.aroundj2me.location.LocationService;
import com.aroundj2me.model.ContinentName;
import com.aroundj2me.model.Place;

public class LocationServiceTest extends TestCase {
	private LocationService locationService;
	private Landmark landmark;
	private QualifiedCoordinates landmarkCoordinates;
	private String[] landmarkCategories;
	private AddressInfo addressInfo;
	private Coordinates currentCoordinates;
	
	public LocationServiceTest() {
	}
	
	public LocationServiceTest(String name, TestMethod method) {
		super(name, method);
	}
	
	protected void setUp() throws IOException, LandmarkException {
		locationService = new LocationService("AroundJ2Me-Test");
		locationService.init();
		
		landmarkCategories = new String[1];
		landmarkCategories[0] = "Bars";
		
		//create landmarkStore for EUROPE
		locationService.createLandmarkStore(ContinentName.EUROPE);
		
		//Create landmark for Unibar
		landmarkCoordinates = new QualifiedCoordinates(46.499855, 11.352568, 0f, 0f, 0f);
		addressInfo = new AddressInfo();
		addressInfo.setField(AddressInfo.CITY, "Bozen");
		landmark = new Landmark("Unibar", "Bar at the Free University of Bolzano-Bozen", landmarkCoordinates, addressInfo);
		
		//Set current position to coordinates of Waltherplatz - Piazza Walther
		currentCoordinates = new Coordinates(46.4982135, 11.354706, 0f);
	}
	
	protected void tearDown() throws IOException, LandmarkException {
		currentCoordinates = null;
		landmark = null;
		addressInfo = null;
		landmarkCoordinates = null;	
	}
	
	public void testAddRetrieveLandmark() throws IOException, LandmarkException {
		locationService.addLandmark(ContinentName.EUROPE, landmark, landmarkCategories);
		
		Vector nearbyPlaces = locationService.findNearbyLandmarks(currentCoordinates);
		assertNotNull("nearby places shouldn't be null",nearbyPlaces);
		
		assertEquals("Only one nearby place should be found", 1, nearbyPlaces.size());
		Place place = (Place)nearbyPlaces.elementAt(0);
		assertNotNull("Place should be not null", place);
		assertEquals("Place name should be Unibar", "Unibar", place.getLandmark().getName());
		
		nearbyPlaces = locationService.findNearbyLandmarks(currentCoordinates, "Bars");
		assertNotNull("nearby places (landmarks) shouldn't be null",nearbyPlaces);
		assertEquals("Only one nearby place should be found", 1, nearbyPlaces.size());
		place = (Place)nearbyPlaces.elementAt(0);
		assertNotNull("Place should be not null", place);
		assertEquals("Place name should be Unibar", "Unibar", place.getLandmark().getName());
		
		nearbyPlaces = locationService.findNearbyLandmarks(currentCoordinates, "Restaurants");
		assertNotNull("nearby places (landmarks-restaurants) shouldn't be null",nearbyPlaces);
		assertEquals("No nearby place should be found", 0, nearbyPlaces.size());		
	}
	
	public Test suite() {
		TestSuite aSuite = new TestSuite();
		aSuite.addTest(new LocationServiceTest("LocationServiceTest:testAddRetrieveLandmark", new TestMethod() { 
			public void run(TestCase tc) throws IOException, LandmarkException {
				((LocationServiceTest) tc).testAddRetrieveLandmark(); 
			} 
		}));
		return aSuite;
	}
}
